package com.sinosoft.cloud.access.router;

import com.sinosoft.cloud.access.net.ClientProperties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;

/**
 * cloud-access
 *
 * @title: cloud-access
 * @package: com.sinosoft.cloud.access.router
 * @author: yangming
 * @date: 2017/11/23 下午9:55
 */
@Controller
public class RefleshPropertiesController {

    @Autowired
    private StandardEnvironment environment;

    private static final Log logger = LogFactory.getLog(RefleshPropertiesController.class);

    @Autowired
    private ClientProperties clientProperties;

    @Autowired
    AutowireCapableBeanFactory beanFactory;

    @Autowired
    private RouterBean routerBean;

    @GetMapping("/router/reflesh")
    @ResponseBody
    public String reflesh() throws IOException {

        Resource resource = clientProperties.getRouter().get("abc").getConfig();

        if (!resource.exists()) {
            return "文件路径错误！错误路径为：" + resource.getURL();
        }
//        String headEl = routerBean.getHeadEL("abc");
        MutablePropertySources propertySources = environment.getPropertySources();
        Iterator<PropertySource<?>> propertySourceIterator = propertySources.iterator();
        PropertySource p = null;
        while (propertySourceIterator.hasNext()) {
            p = propertySourceIterator.next();
            if (p.getName().indexOf(resource.getFilename()) > 0) {
                break;
            }
        }
        Properties properties = new Properties();
        InputStream inputStream = resource.getInputStream();
        properties.load(inputStream);
        inputStream.close();
        propertySources.replace(p.getName(), new PropertiesPropertySource(p.getName(), properties));
        ClientProperties tmp = beanFactory.createBean(clientProperties.getClass());
//        routerBean.setBodyEL(tmp.getBodyEL());
//        routerBean.setHeadEL(tmp.getHeadEL());
//        routerBean.setCrossEL(tmp.getCrossEL());
        clientProperties.setRouter(tmp.getRouter());

        logger.info("动态刷新服务路由成功！" + System.getProperty("line.separator") + "HeadEL：" + routerBean.getHeadEL("abc")
                + System.getProperty("line.separator") + "BodyEl：" + routerBean.getBodyEL("abc")
                + System.getProperty("line.separator") + "CrossEL：" + routerBean.getCrossEL("abc"));

        return routerBean.getHeadEL("abc") + "</br></br>" + routerBean.getBodyEL("abc") + "</br></br>" + routerBean.getCrossEL("abc");
    }
}
